home *** CD-ROM | disk | FTP | other *** search
- Path: news.nask.org.pl!usenet
- From: Roman Habrat <romek@robix.comp.waw.pl>
- Newsgroups: comp.lang.c++
- Subject: Re: BC 3.1 - 4.5 (error!?) ATTENTION - AVOID TRICKS
- Date: Fri, 19 Apr 1996 18:19:57 +0200
- Organization: Comp SA
- Message-ID: <3177BD2D.246B@robix.comp.waw.pl>
- References: <3173D628.48F6@demos.su> <4l5gn1$gtb@Grouper.Exis.Net> <31768AA5.420E@dbs.demos.su>
- NNTP-Posting-Host: robix.comp.waw.pl
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 3.0b2 (X11; I; SunOS 5.3 sun4m)
- CC: 00alex@dbs.demos.su
-
- Alexey Ruzin wrote:
-
- > > >Here is simple example of abnormal programming.
- > > >But BC crashed (sorry, it works and even compiles
- > > >the program, but it is unpredictable what will occur)
- > > >on it:
-
- and later explained:
-
- > Thanks to all, who reply on my message. I think i need to
- > explain some things in hronological sequence.
- > Once my friend wrote a program. He need to store incoming value
- > in some function at first call and do something else with
- > this value at next calls. He wants initialize 'static' variable
- > with incoming value:
- >
- > int func( int a )
- > {
- > static int sv = a;
- > ...
- > }
-
- That is pretty example of so called TRICK-PROGRAMMING (above).
- One tries to use tricks rather than write program clearly and reliably.
-
- How to do it reliably:
-
- int func (int a)
- {
- static int ready = 0; // or FALSE
- static int sv;
-
- if (! ready)
- {
- sv = a;
- ready = 1; // or TRUE
- }
- ...
- }
-
- It works always and with every compiler. You don't waste time trying.
-
- Remember:
- Even if you would test a trick with 20 compilers, on 10 different
- machines, on 10 diferent operating systems, it can fail with
- 21th compiler, 11th machine, or simply with other compliler options
- (one optimization less or more for instance).
-
- There is no ADDITIONAL execution overhead (for testing 'ready') nor
- for memory (for the flag), because it is INEVITABLE.
- What you think, how would the program perform exactly what you want
- without such an overhead ?
- Even if compiler would 'magically' prepare such a code, there always
- must be a flag (apparent or hidden): if it is a first call to the
- function or it is not.
- I am sure it's better to make such a flag apparent.
-
- Of course, if values for 'a' are not from whole 'int' domain
- (but for instance -1000..1000), we can use another value as an
- sentinel:
-
- #define OTHER 1001 // assumption: 'a' cannot take such a value
-
- int func (int a)
- {
- static int sv = OTHER;
-
- if (sv == OTHER) // called first time ?
- sv = a;
- ...
- }
-
- But now we should comment that, because it is kind of a trick.
-
- > (...)
- > Thanks to all for attention, that was my fault.
-
- Don't worry Alexey.
- Usually better to learn something by experimentation,
- then blindly put such a code into big project without testing.
- You experimented and asked people here, it was good job.
-
- ---
- Roman Habrat
-